<?php
namespace app\stores\model\promotion;

use app\common\model\CommonModel;

use app\stores\model\withdraw\MemberWithdrawModel;
use think\facade\Db;

class PromotionmemberModel extends CommonModel{
    protected  $name = 'promotion_member';
    protected  $pk   = 'id';

    public static function info($where,$order=[]){
        $info = Db::name('promotion_member')->where($where)->order($order)->find();
        return $info;
    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('promotion_member')->alias('a')
            ->join('promotion_team b','a.team_id=b.id')
            ->join('beautiful_member c','a.member_id=c.member_id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['is_effect']),function ($query) use ($where){
                $query->where(['a.is_effect'=>$where['is_effect']]);
            })
            ->when(!empty($where['team_id']),function ($query) use ($where){
                $query->where(['a.team_id'=>$where['team_id']]);
            })
            ->when(isset($where['type']),function ($query) use ($where){
                $query->where(['a.type'=>$where['type']]);
            })
            ->when(isset($where['is_delete']),function ($query) use ($where){
                $query->where(['b.is_delete'=>$where['is_delete']]);
            })
            ->when(isset($where['name']) && !empty($where['name']),function ($query) use ($where){
                $query->where('a.name','like','%'.$where['name'].'%');
            })
            ->field("a.*,b.name as team_name,b.imgurl,b.username as team_username,b.mobile as team_mobile,c.face,c.member_num");
        $count = $query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v) {
                $p_w = $tj_where = $tx_where = [];
                $return = self::data_statis($v);
                $v['total_money'] = sprintf("%.2f",$return['total_money']);
                $v['ing_money'] = sprintf("%.2f",$return['ing_money']);
                $v['withdraw_money'] = sprintf("%.2f",$return['withdraw_money']);
                $v['member_money'] = $return['member_money'];

                $type=$v['type'];
                $team_id=$v['team_id'];
                $member_id=$v['member_id'];

                //业绩
                $p_w[] = ['team_id','=',$team_id];
                if($type==1){
                    $p_w[] = ['member_id','=',$member_id];
                }else{
                    $p_w[] = ['member_id','=',0];
                }
                $return_statis = PromotionmoneydetailModel::data_statis([],$p_w,$v['team_id']);
                $v['total_performance'] = sprintf("%.2f",$return_statis['total_performance']);

                //待解冻
                $tj_where[] = ['team_id','=',$team_id];
                if($type==1){
                    $tj_where[] = ['member_id','=',$member_id];
                }else{
                    $tj_where[] = ['member_id','=',0];
                }
                $return_statis = PromotionmoneydetailModel::data_statis([],$tj_where,$v['team_id']);
                $v['total_ing_money'] = sprintf("%.2f",$return_statis['total_ing_money']);

                //已提现（到账）
                $tx_where[] = ['type','=',1];
                $tx_where[] = ['status','=',1];
                $tx_where[] = ['team_id','=',$team_id];
                $tx_where[] = ['is_delete','=',0];
                if($type==1){
                    $tx_where[] = ['member_id','=',$member_id];
                }else{
                    $tx_where[] = ['member_id','=',0];
                }

                $v['total_withdraw_money'] = MemberWithdrawModel::where($tx_where)->sum('money');
                $v['total_withdraw_money'] = sprintf("%.2f",$v['total_withdraw_money']);
            }
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
    public static function data_statis($data){
        $member_id = $data['type']==2?0:$data['member_id'];
        //团队业绩/累计佣金
        $return['total_money'] = PromotionmoneydetailModel::where(['type'=>1,'member_id'=>$member_id,'team_id'=>$data['team_id'],'status'=>0])->where('order_id','>',0)->sum('money');
        //待解冻
        $return['ing_money'] = PromotionmoneydetailModel::where(['type'=>1,'member_id'=>$member_id,'team_id'=>$data['team_id'],'status'=>0])->sum('money');
        //已提现(已到账)
        $return['withdraw_money'] = MemberWithdrawModel::where(['type'=>1,'member_id'=>$member_id,'team_id'=>$data['team_id'],'status'=>1])->sum('money');
        //余额
        $record = PromotionrecordModel::info(['member_id'=>$member_id,'team_id'=>$data['team_id']]);

        $return['member_money'] = sprintf("%.2f",empty($record)?0:$record['money']);


        return $return;
    }
}
